<template>
    <el-dialog
            :title="!dataForm.userId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="用户ID" prop="userId">
                        <el-input v-model.number="dataForm.userId" placeholder="用户ID"></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="username">
                        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item label="课程访问量" prop="courseVisitCount">
                        <el-input v-model.number="dataForm.courseVisitCount" placeholder="课程访问量"></el-input>
                </el-form-item>
                <el-form-item label="学习时长（分钟）" prop="learningDuration">
                        <el-input v-model.number="dataForm.learningDuration" placeholder="学习时长（分钟）"></el-input>
                </el-form-item>
                <el-form-item label="活跃度（0-100）" prop="activityLevel">
                        <el-input v-model.number="dataForm.activityLevel" placeholder="活跃度（0-100）"></el-input>
                </el-form-item>
                <el-form-item label="用户留存率" prop="retentionRate">
                        <el-input v-model="dataForm.retentionRate" placeholder="用户留存率"></el-input>
                </el-form-item>
                <el-form-item label="用户流失率" prop="churnRate">
                        <el-input v-model="dataForm.churnRate" placeholder="用户流失率"></el-input>
                </el-form-item>
                <el-form-item label="反馈来源（问卷、评论等）" prop="feedbackSource">
                        <el-input v-model="dataForm.feedbackSource" placeholder="反馈来源（问卷、评论等）"></el-input>
                </el-form-item>
                <el-form-item label="用户反馈内容" prop="feedback">
                        <el-input v-model="dataForm.feedback" placeholder="用户反馈内容"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            userId: null,
            username: null,
            courseVisitCount: null,
            learningDuration: null,
            activityLevel: null,
            retentionRate: null,
            churnRate: null,
            feedbackSource: null,
            feedback: null
          },
          dataRule: {
            userId: [
                        { required: true, message: '用户ID不能为空', trigger: 'blur' }
            ],
            username: [
                        { required: true, message: '用户名不能为空', trigger: 'blur' }
            ],
            courseVisitCount: [
                        { required: true, message: '课程访问量不能为空', trigger: 'blur' }
            ],
            learningDuration: [
                        { required: true, message: '学习时长（分钟）不能为空', trigger: 'blur' }
            ],
            activityLevel: [
                        { required: true, message: '活跃度（0-100）不能为空', trigger: 'blur' }
            ],
            retentionRate: [
                        { required: true, message: '用户留存率不能为空', trigger: 'blur' }
            ],
            churnRate: [
                        { required: true, message: '用户流失率不能为空', trigger: 'blur' }
            ],
            feedbackSource: [
                        { required: true, message: '反馈来源（问卷、评论等）不能为空', trigger: 'blur' }
            ],
            feedback: [
                        { required: true, message: '用户反馈内容不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.userId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.userId = id || 0
            if (this.dataForm.userId) {
              this.$http({
                url: this.$http.adornUrl(`/app/user_analysis/info/${this.dataForm.userId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appuseranalysis
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/user_analysis/${!this.dataForm.userId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>